package chat.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.SoftBevelBorder;

import chat.client.ClientMain;
import chat.user.Message;
import chat.user.MessageCreator;
import chat.user.User;

public class ChatPanel extends JPanel{
	
	private JScrollPane scroll_panel, mes_panel;
	private JTextArea mes_area;
	private JTextArea chat_area = new JTextArea ();
	public User user;
	
	public ChatPanel(User u){
		
		new Thread(new ChatHandler()).start();
		
		setLayout(null);
		setVisible(true);
		JLabel label = new JLabel("This is a default screen");
		label.setBounds(30, 0, 150, 150);
		//add(label);
		
		this.user = u;
		
		mes_area = new JTextArea ();
		
		chat_area.setEditable(false);
		
		
		JButton send_btn = new JButton("Send");
		send_btn.setBounds(255,248,80,30);
		send_btn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				
				String  text = mes_area.getText();
				mes_area.setText("");
				
				Message m = MessageCreator.createTextMessage(text, user);
				
				ClientMain.addOutMessage(m);
			}
		});
		add(send_btn);
		
		
		SoftBevelBorder softborder = new SoftBevelBorder(BevelBorder.RAISED);
		EmptyBorder etched = new EmptyBorder(5,5,5,5);
		CompoundBorder compborder = new CompoundBorder (etched,softborder);
		
		scroll_panel = new JScrollPane(chat_area);
		
		scroll_panel.setBorder(compborder);
		add(scroll_panel);
		scroll_panel.setBounds(0, 0, 340, 250);
		
		mes_panel = new JScrollPane(mes_area);
		
		mes_panel.setBorder(compborder);
		add(mes_panel);
		
		mes_panel.setBounds(0, 245, 250, 100);
		
		//setBounds(0, 0, 300,300);
		
	}
	
	public void setChatText(String text){
		
		chat_area.setText(text);
	}
	public String getChatText(){
		
		return chat_area.getText();
	}
	
	/*public static void main (String []args){
		
		JFrame frame = new JFrame();
		ChatPanel panel = new ChatPanel();
		frame.add(panel);
		frame.setBounds(0, 0, 500, 500);
		
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}*/
}